const userModel = require("../models/userModel")
//加密
const crypto = require('crypto');
const secret = 'abc'; //密钥
module.exports = {
    //查询
    async AllUser() {
        return await userModel.find().exec()
    },
    //查询用户
    async QueryUser(name, pwd, photo) {
        return await userModel.findOne({
            name,
            pwd: crypto.createHmac('sha256', secret)
                .update(pwd) //密码
                .digest('hex'),
            photo
        }).exec()
    },
    //注册用户
    async RegisteredUsers({
        name,
        pwd,
        photo
    }
    ) {
        return await userModel.create({
            name: name,
            pwd: crypto.createHmac('sha256', secret)
                .update(pwd) //密码
                .digest('hex'),
            photo: photo
        })
    },
    async QueryName(name) {
        return await userModel.findOne({
            name
        }).exec()
    },
    
    //删除用户
    async deleteUser(usersId) {
        return await userModel.deleteOne({
            '_id':usersId
        }).exec()
    },


    // 查看完整信息
    async completeInformation(userId) {
        return await userModel.findById(userId).exec()
    },

    //修改用户
    async modifyUser({'_id':userId},{name,pwd,photo}) {
        return await userModel.updateOne({'_id':userId}, { 
            name,
            pwd,
            photo
        }).exec()
    },

    //分页
    // currentPage:当页页数
    // pageSize： 每页显示的条数 
    async accessToPag({
        currentpage,
        pagesize
    }) {
        return await userModel.find()
            .skip((currentpage - 1) * pagesize)
            .limit(pagesize)
            .sort({
                '_id': -1
            })
            .exec()
    },
    //数据的总条数
    async getsNumber() {
        return userModel.countDocuments().exec()
    },


}